package com.nick.pay.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: PaymentController
 * @Descpriction: 支付控制器
 * @author: zhangchao
 * @date: 2020/4/28 10:24
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("pay")
public class PaymentController {
    @Value("${server.port}")
    private String port;

    @Value("${profile}")
    private String profile;

    // 注入服务发现客户端
    @Autowired
    private DiscoveryClient client;

    @HystrixCommand(fallbackMethod = "fallback",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2")
            })
    @RequestMapping("/go")
    public String go() {
        log.info("go调用....");
//        throw new RuntimeException();
        return "go pay success,port:" + port;
    }

    @RequestMapping("/go2")
    @HystrixCommand(fallbackMethod = "fallback",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true")},
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "500"),
                    @HystrixProperty(name = "maxQueueSize", value = "20"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "20")
            })
    public String go2() {
        try {
            log.info("go2 调用...");
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "go2 pay success";
    }


    @RequestMapping("profile")
    public String profile() {
        return "当前环境：" + profile;
    }

    public String fallback() {
        return "system error~";
    }
}
